import commonConstants from "@/common/commonConstants";
import MonitorManager from "./MonitorManager";
import { ScreenSpaceEventHandler, ScreenSpaceEventType } from "cesium";
import commonMethod from "@/common/commonMethod";

class DefaultMonitor{
   
    constructor(viewer){
        this.curViewer = viewer;
        this.curScene = this.curViewer.scene;
        this.curCanvas = this.curScene.canvas;
        this.allEntities = [];
        this.isActive = false;
        this.monitorKey = commonConstants.monitorKey.Default;
        
        this.drawHandler = undefined;
        this.monitorManager = undefined;
        this.curRightMenu = undefined;
    }

    /**
     * 
     * @param {Boolean} bool 
     */
    setActive(bool){
        let _this = this;
        _this.isActive = bool;
    }

    setCurRigthMenu(rightMenu){
        let _this = this;
        _this.curRightMenu = rightMenu;
    }

    /**
     * 
     * @param {MonitorManager} manager 
     */
    setManager(manager){
        let _this = this;
        _this.monitorManager = manager;
    }

    putEntity(entity){
        let _this = this;
        _this.allEntities.push(entity);
    }

    removeEntityfromEntities(entity){
        let _this = this;
        const number = _this.allEntities.indexOf(entity);
        if (number !== -1){
            _this.allEntities.splice(number,1);
        }
    }

    destoryRightMenu(){
        let _this = this;
        if (_this.curRightMenu){
            _this.curRightMenu.destroy();
            _this.curRightMenu = null;
        }
    }

    /**
     * 部署之法，是主要方法，进行监听
     */
    deploy(){
        let _this = this;
        if (!_this.drawHandler){
            _this.drawHandler = new ScreenSpaceEventHandler(_this.curCanvas);
               
            _this.drawHandler.setInputAction(event => {

                if (!_this.isActive){
                    return;
                }
             
                _this.destoryRightMenu();
                let judgeClickPosition = commonMethod.judgeClickPosition(event.position,_this.curViewer);
                if (!judgeClickPosition){
                    return;
                }

                let clickEntity = commonMethod.judgeClickEntity(_this.curViewer,judgeClickPosition.pos);
                if (!clickEntity){
                    return;
                }
                _this.pickEntity = _this.cosverEntity(clickEntity);
                if(!_this.pickEntity){
                    return;
                }

                let monitor = _this.monitorManager.getMonitorByKey(_this.pickEntity.monitorKey);
                if (monitor) {
                    monitor.handerEvent(event, _this.pickEntity, ScreenSpaceEventType.LEFT_CLICK);
                    _this.monitorManager.setCurMonitor(monitor);
                }

            },ScreenSpaceEventType.LEFT_CLICK);

            _this.drawHandler.setInputAction(event =>{

                if (!_this.isActive){
                    return;
                }

                _this.destoryRightMenu();
                let judgeClickPosition = commonMethod.judgeClickPosition(event.position,_this.curViewer);
                if (!judgeClickPosition){
                    return;
                }

                let clickEntity = commonMethod.judgeClickEntity(_this.curViewer,judgeClickPosition.pos);
                if (!clickEntity){
                    return;
                }
              
                _this.pickEntity = _this.cosverEntity(clickEntity);
                let monitor = _this.monitorManager.getMonitorByKey(_this.pickEntity.monitorKey);
                if (monitor){
                    monitor.handerEvent(event,_this.pickEntity,ScreenSpaceEventType.RIGHT_CLICK);
                }
            },ScreenSpaceEventType.RIGHT_CLICK);

        }
    }

    /**
     * 对点击获得的对象，筛选，不是自己创造的实体，不能算选中
     * @param {*} entity 
     * @returns 
     */
    cosverEntity(entity){
        let _this = this;
        let findEntity = undefined;
        const entityType = entity.entityType;
        if (entityType === commonConstants.entityType.marker && entity.pid){
            // 合成的entity
           //在集合中查找是否有对应的Entity
            for (let i = 0; i < _this.allEntities.length; i++) {
                let entityArrayElement = _this.allEntities[i];
                if (entityArrayElement.getEntityId()){
                    if (entity.pid === entityArrayElement.getEntityId()){
                        findEntity = entityArrayElement;
                        break;
                    }
                }
            }
        }else{
            findEntity = entity;
        }
        return findEntity
    }

};

export default DefaultMonitor;